/**  
* @Title: OrderController.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年4月10日  
*/
package com.fortune.rainshine.service.controller;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.service.service.OrderService;
import com.fortune.rainshine.service.service.ShopService;
import com.fortune.rainshine.system.service.UserService;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.service.OrderDetailModel;
import com.fortune.rainshinecommon.model.service.OrderModel;
import com.google.gson.Gson;

/**  
* @ClassName: OrderController  
* @Description: TODO(采购订单管理)  
* @author 张鑫
* @phone 17561783855
* @date 2019年4月10日  
*    
*/
@Controller
@RequestMapping("/service/orderBuy")
public class OrderBuyController extends BaseController {
	private final static Logger logger = LoggerFactory.getLogger(ProductController.class);
	@Autowired
	private OrderService service;

	@RequiresPermissions("orderBuy:view")
	@RequestMapping()
	public String buyProduct(Model model, HttpServletRequest request) {
		//获取用户权限
		Set<String> authorities = getLoginUserAuthorities();
		//获取登录用户的按钮展示情况
		model.addAttribute("edit", authorities.contains("orderBuy:detailForm"));
		model.addAttribute("confirming",authorities.contains("orderBuy:confirming"));
		model.addAttribute("receiving", authorities.contains("orderBuy:receiving"));
		return "order/orderBuyList.html";
	}

	/**
	 * @Title: list
	 * @Description: 订单列表
	 * @author zhangxin
	 * @date 2019年3月26日
	 */
	@RequiresPermissions("orderBuy:view")
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public MsgModel buyList(Integer page, Integer limit, String beginDate, String endDate,Integer shopId) {
		try {
			if (page == null) {
				page = 0;
				limit = 0;
			}
			ParmsModel parm = new ParmsModel(beginDate,endDate,page,limit,shopId);
			parm.setType(1);//判断为获取采购列表

			return service.list(parm);
		} catch (Exception e) {
			logger.error("OrderBuyController buyList ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}

	}
	
	
	
	@RequiresPermissions("orderBuy:detailForm")
	@RequestMapping("/detailForm")
	public String detailForm(String orderId,Model model) {
		List<OrderDetailModel> orders = service.findDetailOrders(orderId);
		model.addAttribute("orders",new Gson().toJson(orders));
		return "order/orderDetail.html";
	}
	
	/**
	 * @Title: receiving
	 * @Description: 买方确认收货
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年5月14日
	 */
	@RequiresPermissions("orderBuy:receiving")
	@ResponseBody
	@RequestMapping("/receiving")
	public MsgModel receiving(@RequestBody OrderModel model) {
		try {
			model.setUser(this.getLoginUser());
			return service.receiving(model);
		} catch (Exception e) {
			logger.error("OrderBuyController receiving ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}
	}
	
	/**
	 * @Title: cancelOrder
	 * @Description: 买方取消订单
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年5月30日
	 */
	@RequiresPermissions("orderBuy:cancel")
	@ResponseBody
	@RequestMapping("/cancel")
	public MsgModel cancelOrder(@RequestBody OrderModel model) {
		try {
			model.setUser(this.getLoginUser());
			return service.cancelOrder(model);
		} catch (Exception e) {
			logger.error("OrderBuyController cancelOrder ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}
	}
	
	
	
	
}
